
/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-04  21:51
 * @Description : 剑指 Offer 04. 二维数组中的查找
 * @Version : 1.0
 */

public class Num04_findNumberIn2DArray {

    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix.length == 0 || matrix[0].length == 0) return false;
        int row = 0; //列
        int col = 0;// 行
        int R = matrix[0].length - 1;
        int C = matrix.length - 1;
        while (col <= C) {
            if (matrix[col][R] == target) {
                return true;
            } else if (matrix[col][R] < target) {
                col++;
            } else {
                row = R;
                while (row >= 0) {
                    if (matrix[col][row] == target) {
                        return true;
                    }
                    row--;
                }
                col++;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 4, 7, 11, 15}};
        boolean numberIn2DArray = findNumberIn2DArray(arr, 1);
        System.out.println(numberIn2DArray);
    }
}
